package util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Files {

    private static final int BUF_SIZE = 0x1000; // 4K

    public static void copy(File from, File to) throws IOException {
        FileInputStream input = new FileInputStream(from);
        FileOutputStream output = new FileOutputStream(to);
        byte[] buf = new byte[BUF_SIZE];
        while (true) {
            int r = input.read(buf);
            if (r == -1) {
                break;
            }
            output.write(buf, 0, r);
        }
        
        input.close();
        output.close();
    }

    public static void move(File from, File to) throws IOException {
        if (!from.renameTo(to)) {
            copy(from, to);
            if (!from.delete()) {
                if (!to.delete()) {
                    throw new IOException("Unable to delete " + to);
                }
                throw new IOException("Unable to delete " + from);
            }
        }
    }
    
    public static String read(File file) throws IOException{
    	BufferedReader br = new BufferedReader(new FileReader(file));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append("\n");
                line = br.readLine();
            }
            return sb.toString();
        } finally {
            br.close();
        }
    }
    
    public static void unzip(InputStream inputStream, File outputFile, String extensionFilter)
			throws FileNotFoundException, IOException {
		ZipInputStream zis = new ZipInputStream(inputStream);
		ZipEntry ze;
		while ((ze = zis.getNextEntry()) != null) {
			if (ze.getName().endsWith(extensionFilter)) {
				byte[] buff = new byte[1024];
				// get file name
				FileOutputStream fos = new FileOutputStream(outputFile);
				int l = 0;
				// write buffer to file
				while ((l = zis.read(buff)) > 0) {
					fos.write(buff, 0, l);
				}
				fos.close();
			}
		}
		zis.close();
	}
    
    public static boolean isZipFile(File file) throws IOException {
        if(file.isDirectory()) {
            return false;
        }
        if(!file.canRead()) {
            throw new IOException("Cannot read file "+file.getAbsolutePath());
        }
        if(file.length() < 4) {
            return false;
        }
        return isZipFile(new FileInputStream(file));
    }
    
    public static boolean isZipFile(InputStream inputStream) throws IOException {
        DataInputStream in = new DataInputStream(new BufferedInputStream(inputStream));
        int test = in.readInt();
        in.close();
        return test == 0x504b0304;
    }

}
